const { createWebAPIRequest } = require('../../util/util')
module.exports = [
  {
    /**
    * @api {get} /music/playlist/detail [获取歌单内列表]
    * @apiGroup Music
    * @apiPermission none
    * @apiParam {Number} id
    */
    url: '/music/playlist/detail',
    request: (req, res) => {
      const cookie = req.get('Cookie') ? req.get('Cookie') : ''
      const data = {
        id: req.query.id,
        n: 100000,
        csrf_token: ''
      }

      createWebAPIRequest(
        'music.163.com',
        `/api/playlist/detail?id=${req.query.id}`,
        'POST',
        data,
        cookie,
        musicReq => {
          // console.log(JSON.parse(musicReq).playlist.tracks.length)
          // console.log(JSON.parse(musicReq).playlist.trackIds.length)
          res.send(musicReq)
        },
        err => {
          res.status(500).send({err})
        }
      )
    }
  }, {
    /**
    * @api {get} /music/playlist/tracks [歌单收藏或删除歌曲]
    * @apiGroup Music
    * @apiPermission none
    * @apiDescription 单个删除pid,多个删除tracks
    * @apiParam {String} op=del op=del,add
    * @apiParam {Number} pid 歌单id
    * @apiParam {Number} tracks 歌单id
    */
    url: '/music/playlist/tracks',
    request: (req, res) => {
      const {op, pid, tracks} = req.query
      const cookie = req.get('Cookie') ? req.get('Cookie') : ''
      // console.log('COOKIESS', cookie)
      const data = {
        op: op || 'del',
        pid,
        tracks,
        trackIds: JSON.stringify([tracks]),
        csrf_token: ''
      }
      createWebAPIRequest(
        'music.163.com',
        '/weapi/playlist/manipulate/tracks',
        'POST',
        data,
        cookie,
        musicReq => res.send(musicReq),
        err => res.status(500).send({err})
      )
    }
  }, {
    /**
    * @api {get} /music/playlist/hot [热门歌单列表]
    * @apiGroup Music
    * @apiPermission none
    */
    url: '/music/playlist/hot',
    request: (req, res) => {
      const cookie = req.get('Cookie') ? req.get('Cookie') : ''
      const data = {}
      createWebAPIRequest(
        'music.163.com',
        '/weapi/playlist/hottags',
        'POST',
        data,
        cookie,
        musicReq => {
          res.send(musicReq)
        },
        err => res.status(500).send({err})
      )
    }
  }, {
    /**
    * @api {get} /music/playlist/catlist [歌单分类列表]
    * @apiGroup Music
    * @apiPermission none
    */
    url: '/music/playlist/catlist',
    request: (req, res) => {
      const cookie = req.get('Cookie') ? req.get('Cookie') : ''
      const data = {
        csrf_token: ''
      }
      createWebAPIRequest(
        'music.163.com',
        '/weapi/playlist/catalogue',
        'POST',
        data,
        cookie,
        musicReq => {
          res.send(musicReq)
        },
        err => res.status(500).send({err})
      )
    }
  }
]
